import { resultError, resultSuccess } from "../_util";

function createFakeUserList() {
  return [
    {
      userId: "1",
      username: "admin",
      realName: "Super Admin",
      desc: "manager",
      password: "123456",
      access_token: "@guid",
      avatar: "@image('80x80', '#2d8cf0', '#FFF', 'admin')",
      roles: [
        {
          name: "Super Admin",
          value: "admin"
        }
      ]
    },
    {
      userId: "2",
      username: "test",
      password: "123456",
      realName: "test user",
      desc: "tester",
      access_token: "@guid",
      avatar: "@image('80x80', '#2d8cf0', '#FFF', 'test')",
      roles: [
        {
          name: "Tester",
          value: "test"
        }
      ]
    }
  ];
}

export default [
  {
    url: "/user/login",
    timeout: 1000,
    method: "post",
    response: ({ body }) => {
      const { username, password } = body;

      const checkUser = createFakeUserList().find((item) => item.username === username && password === item.password);

      if (!checkUser) {
        return resultError("帐号或密码错误!");
      }
      const { userId, username: cname, access_token, realName, desc, roles, avatar } = checkUser;
      return resultSuccess({
        roles,
        userId,
        username: cname,
        token: access_token,
        realName,
        desc,
        avatar
      });
    }
  },
  {
    url: "/user/logout",
    timeout: 200,
    method: "get",
    response: () => {
      return resultSuccess("退出成功");
    }
  }
];
